import type { User } from '@/types/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'cp-user',
  () => {
    // 1. 用户信息状态
    const user = ref<User>()
    // 2. 设置用户信息的函数n
    const setUser = (u: User) => {
      user.value = u
    }
    // 3. 删除用户信息的函数
    const delUser = () => {
      user.value = undefined
    }

    const returnUrl = ref('')
    const setReturnUrl = (url: string) => (returnUrl.value = url)

    return { user, setUser, delUser, returnUrl, setReturnUrl }
  },
  {
    //开启数据持久化，默认以store的ID作为存储的key
    persist: true
    // 自定义存储的key，指定存储哪些数据
    //persist:{
    // key:'cp-user-150',
    // paths:['user']    数组内放入的变量名都会持久化存储
  }
)
